package com.roberto.main;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.TimeUtils;
import com.roberto.screens.AbstractScreen;
import com.roberto.screens.GameLoop;
import com.roberto.screens.MainMenu;

public class Main extends Game {
    public static final int WIDTH = 1024;
    public static final int HEIGHT = 768;
    public static Texture megaTexture, backgroundTexture;
    public static Preferences pref;
    long start = Main.getCurrentTime();

    @Override
    public void render() {

        AbstractScreen currentScreen = getScreen();

        currentScreen.render(Gdx.graphics.getDeltaTime());

        if (currentScreen.isDone()) {
            currentScreen.dispose();

            if (currentScreen instanceof MainMenu) {
                setScreen(new GameLoop());
            } else {
                setScreen(new MainMenu());
            }
        }

        /* if (Main.getCurrentTime() - start >= 1000) {
             System.out.printf("%.5f %.5f %.5f\n", Gdx.app.getNativeHeap() / 100000.0, Runtime.getRuntime().totalMemory() / 100000.0,
                     Runtime.getRuntime().freeMemory() / 100000.0);
             start = Main.getCurrentTime();
         }*/
    }

    @Override
    public void create() {
        pref = new Preferences();
        megaTexture = new Texture(Gdx.files.internal("res/textures/MegaTexture.png"));
        backgroundTexture = new Texture(Gdx.files.internal("res/textures/background.png"));
        setScreen(new MainMenu());
    }

    /**
     * For this game each of our screens is an instance of InvadersScreen.
     * 
     * @return the currently active {@link AbstractScreen}.
     */
    @Override
    public AbstractScreen getScreen() {
        return (AbstractScreen) super.getScreen();
    }

    public void dispose() {
        megaTexture.dispose();
        backgroundTexture.dispose();
    }

    /** @return current system time in milliseconds in nano precise */
    public static long getCurrentTime() {
        return TimeUtils.nanoTime() / 1000000L;
    }
}
